<?php


namespace app\project\controller;


use app\project\model\BannerModel;
use cmf\controller\AdminBaseController;
use think\Request;

class BannerController extends AdminBaseController
{
    public function index()
    {
        $res = BannerModel::order('id','desc')
            ->paginate(2);
        $this->assign('list',$res);
        return $this->fetch();
    }
    //轮播图后台添加显示
    public function add()
    {
        return $this->fetch();
    }
    //轮播图后台添加
    public function addPost(Request $request)
    {
        $data = $request->param();
        if(empty($data['banner_image']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['banner_name']))
        {
            $this->error('图片名称不能为空');
        }
        if(empty($data['banner_url']))
        {
            $this->error('图片地址不能为空');
        }
        $data['banner_image'] = cmf_get_image_url($data['banner_image']);
        $data['create_time'] = date('Y:m:d H:i:s');
        $res = BannerModel::create($data);
        if($res)
        {
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }
    //轮播图删除
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $res = BannerModel::where('id','=',$id)
            ->delete();
        if($res)
        {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    //轮播图修改
    public function edit(Request $request)
    {
        $id = $request->param('id');
        $res = BannerModel::where('id','=',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch();
    }
    public function editPost(Request $request)
    {
        $data = $request->param();
        if(empty($data['banner_image']))
        {
            $this->error('图片不能为空');
        }
        if(empty($data['banner_name']))
        {
            $this->error('图片名称不能为空');
        }
        $res = BannerModel::where('id','=',$data['id'])
            ->update($data);
        if($res)
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id');
        if(empty($id))
        {
            $this->error('修改失败');
        }
        $res = BannerModel::where('id','=',$id)
            ->find();
        if($res['switch'] == 0)
        {
            BannerModel::where('id','=',$id)
                ->update(['switch'=>1]);
            $this->success('修改成功');
        }else{
            BannerModel::where('id','=',$id)
                ->update(['switch'=>0]);
            $this->success('修改成功');
        }
    }
}